package cn.lena.idle;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.Toast;

import java.util.List;

import cn.lena.idle.adapter.MyCollectionAdapter;
import cn.lena.idle.dao.CollectionDbHelper;
import cn.lena.idle.dao.GoodsDbHelper;
import cn.lena.idle.pojo.DTO.CurrentGoods;
import cn.lena.idle.pojo.DTO.LoginUser;
import cn.lena.idle.pojo.DTO.Version;
import cn.lena.idle.pojo.Goods;

/**
 * @author lena
 * @date 2021/06/15
 */
public class MyOffShelfActivity extends AppCompatActivity {

    private ImageView back;
    private ListView lv;
    private MyCollectionAdapter adapter;
    //private CollectionDbHelper collection_db;
    private GoodsDbHelper goods_db;
    private List<Goods> lists;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_off_shelf);

        back=(ImageView)findViewById(R.id.rb2);
        lv=(ListView)findViewById(R.id.offlv);
        adapter=new MyCollectionAdapter(getApplicationContext());

        // 初始化页面
        initPage();

        // 返回上一页 t
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(MyOffShelfActivity.this,MyActivity.class);
                startActivity(intent);
            }
        });

        // 单击某个商品 进入商品详情页 t
        lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Goods current = (Goods)adapter.getItem(position);
                toDetail(current);
            }
        });

        // 长按商品恢复上架
        lv.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
                backSell(position);
                // 返回值如果是false，执行完长按事件还会执行单击事件；如果是true，执行完长按事件不会再执行单击事件。
                return true;
            }
        });


    }

    /**
     * 长按菜单项恢复上架商品
     */
    public void backSell(final int position){
        AlertDialog.Builder builder = new AlertDialog.Builder(MyOffShelfActivity.this);
        builder.setTitle("提示:").setMessage("确定恢复这个商品吗?").setIcon(R.drawable.icon_my).setNegativeButton("取消", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
            }
        }).setPositiveButton("确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
                Goods goods = (Goods) adapter.getItem(position);
                //删除收藏商品项
                int result=goods_db.updateStatus(goods.getId(),0);
                if(result == 1) {
                    Toast.makeText(MyOffShelfActivity.this,"恢复成功!",Toast.LENGTH_SHORT).show();
                    // 下架后刷新页面：重新上查询并刷新
                    lists = goods_db.getPublish(LoginUser.CURRENT_USER.getId());
                    adapter.setData(lists);
                }else{
                    Toast.makeText(MyOffShelfActivity.this,"服务器出错!",Toast.LENGTH_SHORT).show();
                }
            }
        }).show();
    }


    /**
     * 单击跳转到商品详情页面 t
     */
    public void toDetail(Goods goods){
        CurrentGoods.SELECT=goods;
        Intent intent=new Intent(MyOffShelfActivity.this, GoodsDetailActivity2.class);
        startActivity(intent);
    }


    /**
     * 初始化页面：查询下架商品
     */
    private void initPage(){
        goods_db=new GoodsDbHelper(getApplicationContext(),GoodsDbHelper.DB_NAME,null, Version.GOODS);
        lists=goods_db.getDelete(LoginUser.CURRENT_USER.getId(),2);
        if(!lists.isEmpty()) {
            adapter.setData(lists);
            lv.setAdapter(adapter);
        }
        else{
            Toast.makeText(this,"还没有下架的商品",Toast.LENGTH_LONG).show();
        }
    }

}
